﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YuFeng.Repositories;
using YuFeng.Repositories.Banner;

namespace YuFeng.Services.Banner
{
    public interface IBannerService : IBaseService<Models.Banner.Banner>
    {
        Models.Banner.Banner GetBannerById(string id);
        List<Models.Banner.Banner> GetBanners(bool isdeleted);
        IPagedList<Models.Banner.Banner> GetPageBanners(bool? isdeleted, int page = 1, int size = 10);
    }
    public class BannerService : BaseService<Models.Banner.Banner>, IBannerService
    {
        private readonly IBannerRepository _BannerRepository;
        private readonly IUnitOfWork _unitOfWork;
        public BannerService(IBannerRepository BannerRepository, IUnitOfWork unitOfWork)
            : base(BannerRepository, unitOfWork)
        {
            this._BannerRepository = BannerRepository;
            this._unitOfWork = unitOfWork;
        }
        public Models.Banner.Banner GetBannerById(string id)
        {
            var query = _BannerRepository.GetById(id);
            return query;
        }
        public List<Models.Banner.Banner> GetBanners(bool isdeleted)
        {
            var query = _BannerRepository.GetMany(o => o.IsDeleted == isdeleted && o.IsShow == true).OrderBy(o => o.Sort).ToList();
            return query;
        }
        public IPagedList<Models.Banner.Banner> GetPageBanners(bool? isdeleted, int page = 1, int size = 10)
        {
            var query = _BannerRepository.GetAll();
            if (isdeleted.HasValue)
            {
                query = query.Where(e => e.IsDeleted == isdeleted.Value);
            }
            return query.GetPage(o => o.Sort, page, size, false);
        }
    }

}
